package dfbz.com.controller.article;

import dfbz.com.controller.BaseServlet;
import dfbz.com.model.User;
import dfbz.com.service.ArticleService;
import dfbz.com.service.UserService;
import dfbz.com.service.impl.ArticleServiceImpl;
import dfbz.com.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/article/*")
public class ArticleServlet extends BaseServlet {
           private ArticleService articleService=new ArticleServiceImpl();
           private UserService userService=new UserServiceImpl();


    public void list(HttpServletRequest request, HttpServletResponse response){
        try {
            String currentPage = request.getParameter("currentPage");
            String search = request.getParameter("search");
            int page = 1;
            if(null != currentPage){
                page = Integer.parseInt(currentPage);
            }
            if (null != search) {
                String encode = URLEncoder.encode(search, "UTF-8");
                response.sendRedirect(request.getContextPath() + "/view/articleList?page=" + page + "&search=" + encode);
            }else {
                response.sendRedirect(request.getContextPath() + "/view/articleList?page="+page);
            }


        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public  void add(HttpServletRequest request, HttpServletResponse response){

        try {
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String pdate = df.format(new Date());//发布时间
            Integer userId = (Integer)request.getSession().getAttribute("userId");
            Map<String, Object> user= userService.getUserById(userId);
            String  username =(String) user.get("username");
            articleService.add(username,userId,title,content,pdate);
            response.sendRedirect("/article/list");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void showDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Integer id = Integer.parseInt(request.getParameter("id"));//文章的id编号
        Map<String, Object> article = articleService.findById(id);
        Integer uId = (Integer)article.get("uId");//作者的id
        Integer userId=(Integer)request.getSession().getAttribute("userId");
if (uId!=userId){
    articleService.addBrowseCount(id);
}
        List<Map<String,Object>> friends=userService.getFriends(userId,id);
        request.setAttribute("friends",friends);
        System.out.println("friends:"+friends);
         request.setAttribute("article",article);
         request.setAttribute("alreadyCollect",true);
        request.getRequestDispatcher("/view/showDetail").forward(request,response);
    }

    public void addFavorite(HttpServletRequest request, HttpServletResponse response){
        System.out.println("来对地方了");
        //文章id
        String aId = request.getParameter("aId");
        System.out.println("aId:"+aId);
        Object mId = request.getSession().getAttribute("userId");//当前用户id
        if (null!=aId&&null!=mId){
            articleService.addFavorite(Integer.parseInt(aId),Integer.parseInt(mId.toString()));
        }
        try {
            response.getWriter().write("success");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void myCollect(HttpServletRequest request, HttpServletResponse response){
       try{
           String currentPage = request.getParameter("currentPage");
           String search = request.getParameter("search");
           int page = 1;
           if(null != currentPage){
               page = Integer.parseInt(currentPage);
           }
           if (null != search) {
               String encode = URLEncoder.encode(search, "UTF-8");
               response.sendRedirect(request.getContextPath() + "/view/showMyCollects?page=" + page + "&search=" + encode);
           }else {
               //刚登陆的用户来到这里
               response.sendRedirect(request.getContextPath() + "/view/showMyCollects?page="+page);
           }
       }catch (Exception e){
           e.printStackTrace();
       }

    }


    public void remove(HttpServletRequest request, HttpServletResponse response){


        try {
            String aId = request.getParameter("aId");//文章id
            String userId = request.getSession().getAttribute("userId").toString();//用户id
            if (null!=aId){
                articleService.remove(Integer.parseInt(aId),Integer.parseInt(userId));
            }
            response.getWriter().write("success");


        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void removeFavorite(HttpServletRequest request, HttpServletResponse response){


        try {
            String aId = request.getParameter("aId");//文章id
            String userId = request.getSession().getAttribute("userId").toString();//用户id
            if (null!=aId){
                articleService.removeFavorite(Integer.parseInt(aId),Integer.parseInt(userId));
            }
            response.getWriter().write("success");


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
